package com.besttone.controller;

import com.besttone.dao.HiveJdbcClient;
import com.besttone.dao.ImpalaJdbcClient;
import com.besttone.enums.EnumAPIResultCode;
import com.besttone.util.JsonUtil;
import com.besttone.util.MD5Util;
import com.besttone.vo.HangupSmsResultVO;
import com.besttone.vo.HangupSmsVO;
import com.besttone.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by zhengzhenzhen on 2017/4/18.
 * 挂机短信时间段(按天)统计
 * http://localhost:8888/hangupSms/{"streaming_no":"201704261212000001","sp_account":"test","sp_pwd":"test","called_number":"02022222222","day_from":"20170410","day_to":"20170503","current_page":"1","per_page_size":"10"}
 */

@RestController
@RequestMapping("/hangupSms")
public class HangupSmsController {
    private static Log _log = LogFactory.getLog(HangupSmsController.class);
    private ResultVO resultVO=new ResultVO();
    @Autowired
    private Environment env;
    @RequestMapping("/{queryJson}")
    public String index(@PathVariable("queryJson") String queryJson)
    {
        _log.info("hangupSms queryJson:"+queryJson);
        HangupSmsVO smsVO=JsonUtil.jsonStringToObject(queryJson, HangupSmsVO.class);
        StringBuffer pwdBuf=new StringBuffer();
        String nowpwd= MD5Util.md5(pwdBuf.append(smsVO.getStreaming_no()).append("&").append(smsVO.getSp_account())
                .append("&").append("sms").toString());//加密方式
        /**if(!nowpwd.equals(smsVO.getSp_pwd())){
            resultVO.setResult_code(EnumAPIResultCode.UNAUTHORIZED_BUSINESS.getValue());
            resultVO.setResult_msg("鉴权失败:密码错误");
            return JsonUtil.ObjectToJsonString(resultVO);
        }
        if (StringUtils.isBlank(smsVO.getSp_account())) {
            resultVO.setResult_code(EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
            resultVO.setResult_msg("账号不能为空");
            return JsonUtil.ObjectToJsonString(resultVO);
        }
        if (StringUtils.isBlank(smsVO.getCalled_number())) {
            resultVO.setResult_code(EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
            resultVO.setResult_msg("被叫不能为空");
            return JsonUtil.ObjectToJsonString(resultVO);
        }**/
        String current_page = StringUtils.isBlank(smsVO.getCurrent_page()) ? "1"
                : smsVO.getCurrent_page();// 当前页数
        String per_page_size = StringUtils.isBlank(smsVO.getPer_page_size()) ? "10"
                : smsVO.getPer_page_size();// 每页条数，默认10
        //order by必须写;limit后面为pageSize，offset后面为（currentPage-1）*pageSize
        int offset=(Integer.valueOf(current_page)-1)*Integer.valueOf(per_page_size);
        String sql="select * from ta_sms_day where send_number='02022222222' and day_id<='20170503' and day_id>='20170410' order by day_id asc  limit  "+per_page_size+"  offset  " +offset;
        //String sql1="select * from ti_huawei_log where day_id='20170410' order by day_id asc limit 10 offset 10;";
        List<HangupSmsResultVO> voList= ImpalaJdbcClient.queryHangupSmsByDay(sql);
        resultVO.setResult_code(EnumAPIResultCode.SUCCESS.getValue());
        resultVO.setResult_msg(JsonUtil.ObjectToJsonString(voList));
        return JsonUtil.ObjectToJsonString(resultVO);
    }

}
